﻿using ChatBackend.Entity;
using ChatBackend.Services.TestService;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using Serilog.Context;
using SqlSugar;
using System.Reflection;

namespace ChatBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class TestController : ControllerBase
    {
        private readonly ITestService _testService;
        private readonly ILogger<TestController> _logger;
        private readonly ISqlSugarClient _sqlSugarClient;

        public TestController(ITestService testService, ILogger<TestController> logger, ISqlSugarClient sqlSugarClient)
        {
            _testService = testService;
            _logger = logger;
            _sqlSugarClient = sqlSugarClient;
        }

        [HttpGet]
        public string return123()
        {
            return _testService.return123();
        }

        /// <summary>
        /// 库表生成
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task initDB()
        {
            //创建库
            _sqlSugarClient.DbMaintenance.CreateDatabase();
            var types = Assembly.Load("ChatBackend.Entity").GetTypes().Where(x => x.GetCustomAttributes(typeof(SugarTable), true).Any()).ToArray();
            _sqlSugarClient.CodeFirst.InitTables(types);

            await Task.CompletedTask;
        }

        /// <summary>
        /// 测试新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddTest(TestEntity para)
        {
            return await _testService.AddTest(para);
        }

        /// <summary>
        /// 测试查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<TestEntity>> QueryTestList()
        {
            return await _testService.QueryTestList();
        }

        /// <summary>
        /// 测试删除
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteTest(string Id)
        {
            return await _testService.DeleteTest(Id);
        }

        /// <summary>
        /// 测试修改
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> ModifyTest(TestEntity para)
        {
            return await _testService.ModifyTest(para);
        }
    }
}